package com.study.mybatis;

import com.study.mybatis.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;
import java.sql.*;
import java.util.*;

public class Test2 {

    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static {
        try {
            reader = Resources.getResourceAsReader("Configure.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static SqlSessionFactory getSession() {
        return sqlSessionFactory;
    }

    public static void main1(String[] args) throws ClassNotFoundException, SQLException {
        SqlSession session = getSession().openSession();
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("username", "zhangsan");
        paramMap.put("password", "1");
        Integer userCount = (Integer) session.selectOne("com.study.mybatis.model.UserMapper.finOneBtUserNameAndPwd", paramMap);
        System.out.println(userCount);
    }

    public static void main2(String[] args) throws ClassNotFoundException, SQLException {
        SqlSession session = getSession().openSession();
        List<User> userList = session.selectList("com.study.mybatis.model.UserMapper.findAll");
        for (User user : userList) {
            System.out.println(user);
        }
    }

    public static void main3(String[] args) {
        User user = new User();
        int now = (int)(System.currentTimeMillis()/1000);
        user.setCreateTime(now);
        user.setUpdateTime(now);
        user.setUserGid(UUID.randomUUID().toString());
        user.setUsername("lisi");
        user.setPassword("123456");
        SqlSession session = getSession().openSession();
        int saveResult = session.insert("com.study.mybatis.model.UserMapper.insert", user);
        System.out.println("save user " + saveResult);
        // 事务提交（插入、修改。事务提交）
        session.commit();
    }

    public static void main4(String[] args) {
        SqlSession session = getSession().openSession();
        User updateUser = new User();
        updateUser.setUsername("lisi");
        updateUser.setPassword("888888");
        int updateResult = session.update("com.study.mybatis.model.UserMapper.updatePasswordByUserNmae", updateUser);
        System.out.println("update user " + updateResult);
        session.commit();
    }

    public static void main5(String[] args) {
        SqlSession session = getSession().openSession();
        User deleteUser = new User();
        deleteUser.setUsername("lisi");
        session.delete("com.study.mybatis.model.UserMapper.deleteByUserName", deleteUser);
        session.commit();
        System.out.println("delete user" + deleteUser);
    }

    public static void main(String[] args) {
        SqlSession session = getSession().openSession();

        List<User> users = new ArrayList<>();
        User user = new User();
        user.setId(1);
        users.add(user);
        List<User> userList = session.selectList("com.study.mybatis.model.UserMapper.selectUserByIds", users);
        System.out.println("select user" + userList);

        List<User> inserUsers = new ArrayList<>();
        for (int i = 0; i < 5; i ++) {
            User insertUser = new User();
            insertUser.setCreateTime(1);
            insertUser.setUpdateTime(1);
            insertUser.setUserGid(UUID.randomUUID().toString());
            insertUser.setUsername("zhangsan" + i);
            insertUser.setPassword("pwd" + i);
            inserUsers.add(insertUser);
        }

        int insertResult = session.insert("com.study.mybatis.model.UserMapper.insertUsers", inserUsers);
        System.out.println("插入" + insertResult + "条记录");
        session.commit();
    }
}
